package com.fcgzs.controller;

import com.fcgzs.entity.Result;
import com.fcgzs.entity.StatusCode;
import com.fcgzs.utils.FastDFSClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Auther: ljj
 * @Description:
 */
@RestController
public class UploadController {

    @Value("${FILE_PATH}")
    private String file_path;


    @PostMapping("/upload")
    public Result uploadFile(@RequestParam(name = "file") MultipartFile multipartFile){
        try {
            //1.获取文件的全名称   1.jpg
            String filename = multipartFile.getOriginalFilename();
            //2.通过全名称获取扩展名
            String extName = filename.substring(filename.lastIndexOf(".") + 1);
            //3.初始化工具类
            FastDFSClient fastDFSClient = new FastDFSClient("classpath:fdfs_client.conf");
            //4.调用上传方法完成上传     group1/M00/00/00/wKi8gGF4hJeAR_loAAAS2IuX2uk415.jpg
            String path = fastDFSClient.uploadFile(multipartFile.getBytes(), extName);
            //5.返回文件的路径
            return new Result(true, StatusCode.OK,file_path+path);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"上传失败");
        }

    }
}
